package cn.study.vue_project_xxx.filter;

import cn.study.vue_project_xxx.common.exception.KnowException;
import cn.study.vue_project_xxx.common.result.ResultSet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
@Slf4j
public class GlobalResultExceptionHandler {

    private static final Integer BAD_CODE = 5000;
    private static final String BAD_MESSAGE = "网络开小差了!~~~";

    @ExceptionHandler(value = KnowException.class)
    public ResultSet<Object> captureKnowException(KnowException knowException) {
        ResultSet<Object> fail = ResultSet.fail(BAD_MESSAGE);
        log.warn(knowException.getMessage());
        fail.setCode(BAD_CODE);
        return fail;
    }
    @ExceptionHandler(value = Exception.class)
    public ResultSet<Object> captureException(Exception e) {
        ResultSet<Object> fail = ResultSet.fail("服务发生异常,请联系系统管理员处理!");
        log.error(e.getMessage());
        fail.setCode(BAD_CODE);
        return fail;
    }

}
